package com.czwx.bee.business.manager.modular.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.czwx.bee.business.manager.modular.sys.mapper.ResourceDao;
import com.czwx.bee.business.manager.modular.sys.model.Resource;
import com.czwx.bee.business.manager.modular.sys.service.ResourceService;
import com.czwx.bee.business.manager.modular.sys.vo.vue.MenuVo;
import org.beetl.sql.core.Params;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.Permission;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private SQLManager sqlManager;

    @Autowired
    ResourceDao resourceDao;


    @Override
    public List<Map> findUserResourceByUid(Long uid,Integer resourceType) {
        String sql="select DISTINCT(a.id),a.url,d.name from bp_resource a join bp_role_resource b on a.id=b.resource_id join bp_user_role c on b.role_id=c.role_id " +
                "join bp_role d on d.id=b.role_id " +
                "where c.user_id=#uid#";
               if(resourceType!=null){
                   sql+=" and a.resource_type=#resourceType#";
               }

        return sqlManager.execute(sql,Map.class, Params.ins().add("uid", uid).add("resourceType",resourceType).map());
    }

    @Override
    public List<MenuVo> getMenu(Long uid) {
        String sql="select DISTINCT(a.id), a.* from bp_resource a join bp_role_resource b on a.id=b.resource_id join bp_user_role c on b.role_id=c.role_id " +
                "join bp_role d on d.id=b.role_id " +
                "where c.user_id=#uid# and a.resource_type=0";
       List<Resource> resources= sqlManager.execute(sql,Resource.class, Params.ins().add("uid", uid).map());
//       List<MenuVo> menuVos=new ArrayList<>();
//        resources.forEach(resource -> {
//             if(resource.getPid()==0){//第一层菜单
//                 MenuVo menuVo=  setMenu( resource);
//                 setChild(resources,resource,menuVo);
//                 menuVos.add(menuVo);
//             }
//        });
        return treeList(resources,0l);
    }

    public List<MenuVo> treeList(List<Resource> resourceList,Long parentId){
        List<MenuVo> list=null;
        for(Resource resource:resourceList){
            if(resource.getPid()==parentId){
                MenuVo menuVo=new MenuVo();
                menuVo.setName(resource.getName());
                menuVo.setTitle(resource.getTitle());
                menuVo.setIcon(resource.getIcon());
                if(StrUtil.isEmpty(resource.getUrl())){
                    menuVo.setIndex("");
                }
                else {
                    menuVo.setIndex(resource.getUrl());
                }
                List<MenuVo> child=treeList(resourceList,resource.getId());
                menuVo.setChild(child);
                if(list==null){
                    list=new ArrayList<>();
                }
                list.add(menuVo);
            }
        }


        return list;
    }


    public MenuVo setMenu(Resource resource){
        MenuVo menuVo=new MenuVo();
        menuVo.setName(resource.getName());
        menuVo.setTitle(resource.getTitle());
        menuVo.setIcon(resource.getIcon());
        if(StrUtil.isEmpty(resource.getUrl())){
            menuVo.setIndex("");
        }
        else {
            menuVo.setIndex(resource.getUrl());
        }
        return  menuVo;
    }

    public MenuVo setChild(List<Resource> resources,Resource resource,MenuVo menuVo){

          for(Resource rs:resources){
              if(rs.getPid().toString().equals(resource.getId().toString())){
                    MenuVo childVo=setMenu(rs);
                    childVo=this.setChild(resources,rs,childVo);
                    menuVo=addChild(menuVo,childVo);
              }

          }
          return menuVo;
    }

    public MenuVo addChild(MenuVo parent,MenuVo child){
        if(parent.getChild()==null){
            List<MenuVo> menuVos=new ArrayList<>();
            menuVos.add(child);
            parent.setChild(menuVos);
        }
        else {
            parent.getChild().add(child);
        }
        return parent;
    }

    @Override
    public Long insert(Resource resource) {
       Long rs= resourceDao.insertReturnKey(resource).getLong();
        return rs>0?rs:null;
    }

    @Override
    public Boolean update(Resource resource) {
      Integer rs=  resourceDao.updateTemplateById(resource);
        return rs>0?true:false;
    }

    @Override
    public Boolean del(Resource resource) {
        return null;
    }

    @Override
    public Resource getById(Long id) {
        return null;
    }

    @Override
    public List<Resource> getList(Map map) {
        return sqlManager.template(Resource.class,map,"id");
    }

    @Override
    public void getPageList(PageQuery query) {
             resourceDao.templatePage(query);
    }

    @Override
    public Long getCount(Resource resource) {
        return null;
    }

    @Override
    public Resource getBySelf(Resource resource) {
        return null;
    }

    @Override
    public Long saveOrUpdate(Resource resource) {
        if(StrUtil.isEmptyIfStr(resource.getId())) {
            return this.insert(resource);
        }else {
            return this.update(resource)==true?resource.getId():null;
        }
    }
}
